package com.example.actionsignin.fragment;

import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.amap.api.maps.AMap;
import com.amap.api.maps.AMapOptions;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.example.actionsignin.R;
import com.example.actionsignin.mvp.presenter.IMapListener;

/**
 * Created by 旭 on 2017/8/2/002.
 */

public class MapFragment extends Fragment implements
        GeocodeSearch.OnGeocodeSearchListener,
        AMap.OnCameraChangeListener{

    private MapView mapView;
    private AMap aMap = null;
    private Marker mMarker;

    private GeocodeSearch geocodeSearch = null;

    private IMapListener listener = null;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        Bundle bundle = getArguments();
        double lat = bundle.getDouble("latitude");
        double lng = bundle.getDouble("longitude");

        Log.d("TAGG","lat = " + lat);
        Log.d("TAGG","lng = " + lng);

        LatLng latLng = new LatLng(lat,lng);
        AMapOptions options = new AMapOptions();
        options.camera(new CameraPosition(latLng,17,0,0));
        mapView = new MapView(getActivity(),options);
        mapView.onCreate(savedInstanceState);

        if (aMap == null)
        {
            aMap = mapView.getMap();
            aMap.setOnCameraChangeListener(this);

            geocodeSearch = new GeocodeSearch(getActivity());
            geocodeSearch.setOnGeocodeSearchListener(this);

        }

        setMarker(latLng);
//        initUi();

        listener = (IMapListener) getActivity();

        return mapView;
    }

    private void initUi()
    {
        UiSettings uiSettings = aMap.getUiSettings();
        uiSettings.setMyLocationButtonEnabled(true);
    }

    private void setMarker(LatLng latLng)
    {
        MarkerOptions options = new MarkerOptions()
                .icon(BitmapDescriptorFactory.fromBitmap(
                        BitmapFactory.decodeResource(getResources(),R.mipmap.marker)))
                .position(latLng)
                .title("")
                .visible(true)
                .snippet("");
        mMarker = aMap.addMarker(options);
    }

    @Override
    public void onCameraChange(CameraPosition cameraPosition) {
        mMarker.setPosition(cameraPosition.target);
    }

    @Override
    public void onCameraChangeFinish(CameraPosition cameraPosition) {
        LatLonPoint point = new LatLonPoint(cameraPosition.target.latitude,cameraPosition.target.longitude);
        RegeocodeQuery query = new RegeocodeQuery(point,200,GeocodeSearch.AMAP);
        geocodeSearch.getFromLocationAsyn(query);

        listener.setLatLng(cameraPosition.target);
    }

    @Override
    public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
        if (i == com.amap.api.services.core.AMapException.CODE_AMAP_SUCCESS)
        {
            if (regeocodeResult != null
                    && regeocodeResult.getRegeocodeAddress() != null
                    && regeocodeResult.getRegeocodeAddress().getFormatAddress() != null)
            {
                String address = regeocodeResult.getRegeocodeAddress().getFormatAddress();
                Log.d("TAGG","address = " + address);
                listener.setAddress(address);
            }
        }
    }

    @Override
    public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        mapView.onResume();
    }
}
